package com.prj.ufdm.cloud.ms.auth.bpo._impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.prj.ufdm.cloud.ms.auth.bpo.SysDutyBPO;
import com.prj.ufdm.cloud.ms.auth.bpo.SysUserBPO;
import com.prj.ufdm.cloud.ms.auth.dao.SysOrganDao;
import com.prj.ufdm.cloud.ms.auth.dao.SysUserDao;
import com.prj.ufdm.cloud.ms.auth.dao.SysUserRoleDao;
import com.prj.ufdm.cloud.ms.auth.model.SysDuty;
import com.prj.ufdm.cloud.ms.auth.model.SysOrgan;
import com.prj.ufdm.cloud.ms.auth.model.SysRole;
import com.prj.ufdm.cloud.ms.auth.model.SysUser;
import com.prj.ufdm.cloud.ms.core.bpo._impl.BaseBPOImpl;

/**
 * DESC: 系统用户服务类 <br>
 * @author 胡义振
 * @date 2020年1月17日
 */
@Service 
public class SysUserBPOImpl extends BaseBPOImpl<SysUserDao,SysUser> implements SysUserBPO {
	
	@Autowired
	SysUserDao sysUserDao;
	
	@Autowired
	SysUserRoleDao sysUserRoleDao;
	
	@Autowired
	SysDutyBPO sysDutyBPO;
	
	@Autowired
	SysRoleBPOImpl sysRoleBPO;
	
	@Autowired
	SysOrganDao sysOrganDao;
	
	
	/** 
	 * 描述: 通过用户名查询用户信息
	 * @auther 胡义振
	 * @date 2013-8-16 
	 * @param sysUser
	 * @return 
	 */
	public SysUser doGetSysUserByUsername(String username) throws Exception
	{
		SysUser sysUser = sysUserDao.selectSysUserByUsername(username);
		if(sysUser!=null) {			
			/** 查询用户角色信息 */
			String sysRoleIds = "";
			String sysRoleNames = "";
			List<SysRole> listSysRole = sysRoleBPO.doGetSysRoleListByUserId(sysUser.getId());
			if(listSysRole!=null) {
				for(SysRole sysRole : listSysRole) {
					if(sysRoleIds==null || sysRoleIds.equals("")) {
						sysRoleIds = sysRole.getId();
						sysRoleNames = sysRole.getRoleName();
					}
					else {
						sysRoleIds = sysRoleIds + "," + sysRole.getId();
						sysRoleNames = sysRoleNames + "," +sysRole.getRoleName();
					}
				}
			}
			sysUser.setListSysRole(listSysRole);
			sysUser.setSysRoleIds(sysRoleIds);
			sysUser.setSysRoleNames(sysRoleNames);
			
			
			/** 查询用户职务信息 */
			String sysDutyIds = "";
			String sysDutyNames = "";
			List<SysDuty> listSysDuty = sysDutyBPO.doGetListByUserId(sysUser.getId());
			if(listSysDuty!=null) {
				for(SysDuty sysDuty : listSysDuty) {
					if(sysDutyIds==null || sysDutyIds.equals("")) {
						sysDutyIds = sysDuty.getId();
						sysDutyNames = sysDuty.getDutyName();
					}
					else {
						sysDutyIds = sysDutyIds + "," + sysDuty.getId();
						sysDutyNames = sysDutyNames + "," +sysDuty.getDutyName();
					}
				}
			}
			sysUser.setSysDutyIds(sysDutyIds);
			sysUser.setSysDutyNames(sysDutyNames);
			sysUser.setListSysDuty(listSysDuty);
			
			/** 查询用户授权机构信息 */
			String grantOrganIds = "";
			String grantOrganCodes = "";
			String grantOrganNames = "";
			List<SysOrgan> listGrantSysOrgan = sysOrganDao.selectGrantOrganByUserId(sysUser.getId());
			if(listGrantSysOrgan!=null) {
				for(SysOrgan sysOrgan : listGrantSysOrgan) {
					if(grantOrganIds==null || grantOrganIds.equals("")) {
						grantOrganIds = sysOrgan.getId();
						grantOrganCodes = sysOrgan.getOrganCode();
						grantOrganNames = sysOrgan.getOrganName();
					}
					else {
						grantOrganIds = grantOrganIds + "," + sysOrgan.getId();
						grantOrganCodes = grantOrganCodes + "," + sysOrgan.getOrganCode();
						grantOrganNames = grantOrganNames + "," + sysOrgan.getOrganName();
					}
				}
			}
			sysUser.setGrantOrganIds(grantOrganIds);
			sysUser.setGrantOrganCodes(grantOrganCodes);
			sysUser.setGrantOrganNames(grantOrganNames);
			sysUser.setListGrantSysOrgan(listGrantSysOrgan);
			
			return sysUser;

		}
		return null;
	}
	
	/** 
	 * 描述: 通过TOKEN查询用户信息
	 * @auther 胡义振
	 * @date 2013-8-16 
	 * @param token 
	 * @return 
	 */
	public SysUser doGetSysUserByToken(String token) throws Exception
	{
		return sysUserDao.selectSysUserByToken(token);
	}
	
	
	/** 
	 * 描述: 根据用户名、密码获取用户信息
	 * @auther 胡义振
	 * @date 2014-1-9 
	 * @param 
	 *     username 
	 *     password
	 * @return 
	 */
	public SysUser doGetSysUserByUserNameAndPassword(String username,String password) throws Exception
	{
		SysUser sysUser  = new SysUser();
		sysUser.setUsername(username);
		sysUser.setPassword(password);
		return sysUserDao.selectSysUserForLogin(sysUser);
	}
	
	
	/** 
	 * 描述: 检测登录名是否存在（userId 为空，检测新增用户名是否存在。）
	 * @auther 胡义振
	 * @date 2014-1-9 
	 * @param 
	 *     username 用户名
	 *     userId   用户ID
	 * @return 
	 */
	public boolean doCheckSysUserLoginNameExist(String username,String userId) throws Exception
	{
		SysUser sysUser = new SysUser();
		sysUser.setUsername(username);
		//检测新增用户的登录名
		if(userId!=null && !userId.equals("")){
			sysUser.setId(userId);
		}
		Integer searchCount = sysUserDao.selectSysUserForCheckUsername(sysUser);
		if(searchCount!=null && searchCount>0){
			return true;
		}
		return false;
	}

 	/**
 	 * 描述: 通过角色ID查询该角色下的用户
 	 * @auther 胡义振
 	 * @date 2018-03-22
 	 * @param sysUser  角色用户
 	 * @return
 	 */
	@Override
	public List<SysUser> doGetSysUserListWithRoleId(SysUser sysUser) throws Exception {
		return sysUserDao.selectSysUserListWithRoleId(sysUser);
	}
	
 	/**
 	 * 描述: 通过职务ID查询该职务下的用户
 	 * @auther 胡义振
 	 * @date 2018-03-22
 	 * @param sysUser  角色用户
 	 * @return
 	 */
	@Override
	public List<SysUser> doGetSysUserListWithDutyId(SysUser sysUser) throws Exception {
		return sysUserDao.selectSysUserListWithDutyId(sysUser);
	}
	
	/**
	 * 根据用户ID删除系统用户
	 * @param id 要删除的角色ID（多个ID，逗号分隔）
	 * @return
	 * @throws Exception
	 * @author 胡义振  
	 * @date 2018年3月27日
	 */
	@Override
	public Integer doRmSysUserById(String userIds) throws Exception {
		if (userIds == null)  userIds = "";
		String[] arrUserId = userIds.split(",");
		for(String userId : arrUserId){
			// 删除用户角色对应关系
			sysUserRoleDao.deleteUserRoleByUserId(userId);
			// 删除用户表
			sysUserDao.deleteById(userId);
		}
		return 0;
	}
	
	/**
	 * 根据机构id查询用户
	 */
	@Override
	public List<SysUser> doGetSysUserListWithOrganId(SysUser sysUser) throws Exception {
		return sysUserDao.selectSysUserListWithOrganId(sysUser);
	}

	/**
	 * Desc: 查询机构下所有用户(所属机构与机构机构用户)
	 * @param sysUser
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 2019年10月18日
	 * @version 1.0
	 */
	@Override
	public List<SysUser> doGetOrganAllSysUserList(SysUser sysUser) throws Exception {
		return sysUserDao.selectOrganAllSysUserList(sysUser);
	}

}
